class_name UnitFlyFighter
extends UnitBase


# attack
var attack_cooldown_timer: float = 0
@onready var melee_attack_area: Area2D = $ViewRoot/AttackArea
@onready var melee_collision: CollisionPolygon2D = $ViewRoot/AttackArea/CollisionPolygon2D
var melee_hit_targets: Array = []
@onready var shoot_weapon_point: Marker2D = $ViewRoot/ShootWeaponPoint


func pick_new_wander_target():
	# 在矩形范围内随机目标点
	var x_offset = randf_range(-wander_width/2, wander_width/2)
	var y_offset = randf_range(-wander_height/2, wander_height/2)
	target_pos = home_position + Vector2(x_offset, y_offset)
	
	# 下一次切换目标时间
	timer = randf_range(1.0, change_dir_time)


func play_wander_animation():
	animation_player.play("idle")
	

func process_enter_attack(delta):
	if not attack_target: return
	
	var dist = abs(attack_target.global_position.distance_to(global_position)) 
	if dist > unit_data.config.main_attack_range: 
		attack_cooldown_timer = 0
		return
	
	attack_cooldown_timer -= delta
	
	if attack_cooldown_timer <= 0:
		attack_cooldown_timer = unit_data.config.main_attack_cooldown
		
		states.transition("Attack")
	
	
func process_melee_hit():
	if melee_collision.disabled: return
	
	var areas = melee_attack_area.get_overlapping_areas()
	if areas.size() <= 0: return
	
	for area in areas:
		var source = area.source
		if source not in melee_hit_targets:
			melee_hit_targets.append(source)
			source.take_hit(get_melee_hit_data(), self)


func get_melee_hit_data():
	var config = unit_data.config as UnitBaseConfig
	
	var critical_rate = config.main_critical_attack_rate
	var is_critical = randf() <= critical_rate 
	
	var hit_data = HitData.new()
	hit_data.is_critical = is_critical
	hit_data.attack = config.main_attack if not is_critical else config.main_attack * (1.0 + config.main_critical_attack_bonus)
	hit_data.knockback = config.main_attack_knockback
	
	return hit_data


func set_is_enemy(is_enemy: bool):
	super.set_is_enemy(is_enemy)
	
	if is_enemy:
		melee_attack_area.set_collision_mask_value(7, true)
		melee_attack_area.set_collision_mask_value(6, false)
	else:
		melee_attack_area.set_collision_mask_value(7, false)
		melee_attack_area.set_collision_mask_value(6, true)


func pause_animation():
	animation_player.pause()
	
	
func melee_attack_cam_shake():
	if not unit_data.config.main_attack_cam_shake: return
	
	var distance_2_cam_center = global_position.distance_to(level.camera.global_position)
	if distance_2_cam_center >  unit_data.config.main_attack_cam_shake_max_distance:
		return
	
	var dist_diff = abs(unit_data.config.main_attack_cam_shake_max_distance - distance_2_cam_center) 
	var rate = dist_diff * 1.0 / unit_data.config.main_attack_cam_shake_max_distance
	
	var target_cam_shake_intensity = rate * unit_data.config.main_attack_cam_shake_intensity
	level.camera.start_shake(target_cam_shake_intensity, unit_data.config.main_attack_cam_shake_duration)
	

func shoot_weapon():
	if not attack_target: return
	
	var config = unit_data.config as UnitBaseConfig
	
	var unit = attack_target as UnitBase
	var target_point_global_position = unit.ray_cast_point.global_position
	var shoot_direction = ray_cast_point.global_position.direction_to(target_point_global_position)
	
	var bullet_inst = (ResourceManager.get_resource(config.shoot_weapon_name) as PackedScene).instantiate() as RigidBody2D
	level.bullets_parent.add_child(bullet_inst)
	bullet_inst.global_position = shoot_weapon_point.global_position
	bullet_inst.rotation = shoot_direction.angle()
	
	bullet_inst.sender = self
	bullet_inst.attack = config.main_attack
	bullet_inst.critical_rate = config.main_critical_attack_rate
	bullet_inst.critical_bonus = config.main_critical_attack_bonus
	bullet_inst.knockback = config.main_attack_knockback
	bullet_inst.hit_cam_shake_intensity = config.main_attack_cam_shake_intensity
	bullet_inst.hit_cam_shake_duration = config.main_attack_cam_shake_duration
	bullet_inst.level = level
	if config.steal_hp_rate:
		bullet_inst.steal_hp_rate = config.steal_hp_rate
	bullet_inst.set_is_enemy(true)
	
	bullet_inst.set_start_velocity(shoot_direction * config.shoot_weapon_start_speed)
	
	
	
	
	
	
